﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FCA.Dto {
    public class Lattice {
        #region Properties
        /// <summary>
        /// top node ?? Please check
        /// </summary>
        public int RootNodeID { get; set; }
        
        public int LastNodeID { get; set; }

        /// <summary>
        /// this property handle the number of concept in dictionary
        /// i.o.w this mean the length of the Concepts dictionary
        /// </summary>
        public int CurrentID { get; set; }
        public int RootID { get; set; }
        /// <summary>
        /// key is concept id
        /// value is concept data
        /// </summary>
        public Dictionary<int, Concept> Concepts { get; set; }
        #endregion

        #region Constructors
        public Lattice() {
            CurrentID = 0;
            Concepts = new Dictionary<int, Concept>();
        }
        #endregion

        /// <summary>
        /// ID get from concept (need to init id for new added concept)
        /// </summary>
        /// <param name="c"></param>
        public void AddConceptWithID(Concept c) {
            //CurrentID++;
            //c.ID = CurrentID;
            //Concepts.Add(CurrentID, c);
            Concepts.Add(c.ID, c);
        }

        /// <summary>
        /// id get from CurrentID (no need to init id for new added concept)
        /// </summary>
        /// <param name="c"></param>
        public void AddConcept(Concept c) {
            CurrentID++;
            c.ID = CurrentID;
            //Concepts.Add(CurrentID, c);
            Concepts.Add(c.ID, c);
        }

        public Concept GetConceptByID(int id) {
            return Concepts.ContainsKey(id) ? Concepts[id] : null;
        }

        public Concept GetConceptByItemSet(ItemSet itemSet) {
            foreach (KeyValuePair<int, Concept> item in Concepts) {
                if (item.Value.Items.IsEqual(itemSet)) {
                    return item.Value;
                }
            }
            return null;
        }

        internal Concept ItemSetIsExistedInLattice(ItemSet itemSet) {
            foreach (KeyValuePair<int, Concept> item in Concepts) {
                //if itemSet is equal with Item.Value then intersect of itemset and item.Value is itemset
                // & length of the intersect is itemset length and item.Value.items.length too
                int tempLength = item.Value.Items.IntersectWith(itemSet).Length();
                if (tempLength == itemSet.Length() && tempLength == item.Value.Items.Length()) {
                    return item.Value;
                }
            }            
            return null;
        }
    }
}
